﻿//页面加载
$(document).ready(function () {
    $("#btnReset").bind("click", btnReset_click);
    $("#btnSearch").bind("click", BindDataList);
    $("#btnCreate").bind("click", btnCreate_click);
    $("#btnDeleteAll").bind("click", btnDeleteAll_click);
    $("#checkall").bind("click", CheckAll_click);
    BindDataList();
})
//绑定数据
function BindDataList() {
    var v_code = $.trim($("#txtOrderCode").val());
    var v_txtCardID = $("#txtCardID").val();
    var v_txtCreateDate = $("#txtCreateDate").val();
    var v_payType = "";
    if ($("#payType").prop("checked") && $("#payType2").prop("checked"))
    {
        v_payType = "";
    }
    else if ($("#payType").prop("checked"))
    {
        v_payType = $("#payType").val();
    }
    else if ($("#payType2").prop("checked")) {
        v_payType = $("#payType2").val();
    }

    var parameters = MakeParameters("OrderController", "GetPageList", decodeURIComponent(v_code), v_txtCardID, decodeURIComponent(v_payType), v_txtCreateDate);
    var page = new Pagger(parameters, BindDataToTable);
    PageIndexChanging(0);
}
function BindDataToTable(result) {
    if (Pager.RecountCount == -1) {
        $("#errorDataTD").show();
        return;
    }
    else {
        $("#errorDataTD").hide();
    }
    var iCount = 0;
    var $table = $("#TableList");
    var $template = $("#template");
    $table.find(".contentRow").remove();
    $.each(result, function (i, n) {
        iCount++;
        var row = $template.clone();
        row.css("display", "");
        //数据项
        row.find("#td_checkbox").html("<input type='checkbox' name='checkbox'  class='chk' value='" + n.AID + "'/>");
        row.find("#td_Code").html("<a href='/Order/Details?Id=" + n.Code + "'>" + n.Code + "</a>");
        row.find("#td_Buyer").html(n.Buyer);
        row.find("#td_BuyerName").html(n.BuyerName);
        if (n.PayType == "计次卡") {
            row.find("#td_Total").html(n.Total + " 次");
            row.find("#td_Payment").html(n.Payment + " 次");

        }
        else {
            row.find("#td_Total").html("￥" + n.Total);
            row.find("#td_Payment").html("￥" + n.Payment);
        }
        row.find("#td_PayType").html(n.PayType);
        row.find("#td_ComeFrom").html(n.ComeFrom);
        row.find("#td_Buyer_Phone").html(n.Buyer_Phone);
        row.find("#td_Buyer_Appointment").html(CutString(n.Buyer_Appointment, 0, 19));
        if (n.Status == 0) {
           
            row.find("#td_Status").html("结单");
            row.find("#td_Status").css("color", "green");
            row.find("#btnSure").css("display", "none");
            row.find("#btnDelete").css("display", "none");
        }
        else if (n.Status == 1) {
            row.find("#td_Status").html("已支付");
            row.find("#td_Status").css("color", "red");
            row.find("#btnSure").bind("click", { ID: n.Code, CardID: n.Buyer }, btnDelete_click);
            row.find("#btnSure").val("确认消费");
            row.find("#btnDelete").css("display", "none");
        }
        else if (n.Status == 2) {
            row.find("#td_Status").html("待支付");
            row.find("#td_Status").css("color", "red");
            row.find("#btnSure").bind("click", { ID: n.Code, CardID: n.Buyer }, btnEdit_click);
            row.find("#btnSure").val("支 付");

            //待支付可作废
            row.find("#btnDelete").css("display", "block");
            row.find("#btnDelete").bind("click", { ID: n.Code, CardID: n.Buyer }, BecomeInvalid);
        }
        else if (n.Status==3){
            row.find("#td_Status").html("作废");
            row.find("#td_Status").css("color", "black");
            row.find("#btnSure").css("display", "none");
            row.find("#btnDelete").css("display", "none");
        }
        //操作
        //row.find("#btnSetMenu").bind("click", { ID: n.AID }, btnSetMenu_click);


        row.attr("class", "contentRow t_c");
        row.appendTo($table);
    });
    if (iCount == 0) {
        $("#emptyDataTD").show();
    }
    else {
        $("#emptyDataTD").hide();
    }
}
//全选
function CheckAll_click() {
    var chk = $(this);
    chk.parent().parent().parent().next().children().find("input:checkbox").attr("checked", chk.prop("checked"));
}
//重置界面
function btnReset_click() {
    $("#txtTitle").val("");
}

//创建
function btnCreate_click() {
    window.location.href = "/Order/Create";
}

//去支付
function btnEdit_click(e) {
    var Id = e.data.ID;
    window.location.href = "/Order/Pay/" + Id+"?CardID="+e.data.CardID;
}

//  作废订单
function BecomeInvalid(e)
{
    if (confirm("您确定将该订单作废?")) {
        $.post("/Order/BecomeInValid", { orderID: e.data.ID }, function (result) {
            if (result.code == "OK") {
                PageIndexChanging(Pager.CurrentPage);
            }
            else {
                alert(result.msg);
            }
        });
    }
}

//批量删除
function btnDeleteAll_click() {
    if (confirm("您确定该订单客户已经实际消费?")) {
        $.post("/Order/ConfirmPayed", { id: e.data.ID }, function (result) {
            if (result.code == "OK") {
                PageIndexChanging(Pager.CurrentPage);
            }
            else {
                alert(result.msg);
            }
        });
    }
}
//删除单项
function btnDelete_click(e) {
    var Id = e.data.ID;
    if (confirm("您确定该订单客户已经实际消费?")) {
        $.post("/Order/ConfirmPayed", { orderCode: Id }, function (result) {
            if (result.code == "OK") {
                PageIndexChanging(Pager.CurrentPage);
            }
            else {
                alert(result.msg);
            }
        });
    }
}
//时间字符串截取
function CutString(str, index, length) {
    if (str != null && str != undefined) {
        if (str.length >= length) {
            if (length > 10) {
                var str1 = str.substring(index, length);
                return str1.replace("T", " ");
            }
            else {
                return str.substring(index, length);
            }
        }
        else {
            return str;
        }
    }
    else {
        return "";
    }
}
